class bulletManager {
    constructor() {
        this.data = {
            //ID
            //destance
        };
        this.bulletLayer = null;
        this.bulletID = 100;
    }
    setLayer(layer) {
        this.bulletLayer = layer;
    }
    doLaunch() {
        let launchNum = game.role.script.getLaunchNum();
        let angle, flag;
        if (launchNum & 1) {
            angle = 0;
            flag = 1;
        } else {
            angle = -7.5;
            flag = -1;
        }
        for (let i = 0; i < launchNum; i++) {
            let tangle = angle + Math.floor(15 * (i + 1) / 2) * (i & 1 ? -flag : flag);
            this.launch(tangle);
        }
    }
    launch(offsetTangle) {
        if (!this.bulletLayer) {
            return;
        }
        g_Music.playEffict("fire101");
        //获取子弹预处理文件
        let resname = "bullet" + game.getBulletType();
        let pre = g_Res.getRes("bullet", resname);
        let tbullet = cc.instantiate(pre);
        //添加到父节点
        this.bulletLayer.addChild(tbullet);
        //获取角色位置
        let role = game.getRole();
        tbullet.setPosition(role.x, role.y);
        let tangle = role.script.getBodyAngle() + offsetTangle;
        let tspeed = game.getBulletSpeed();
        tbullet.ID = this.bulletID++;
        tbullet.attack = role.script.getAttack();
        this.data[tbullet.ID] = {
            bullet: tbullet,
            angle: tangle,
            speed: tspeed,
            life: 5,
        }
    }
    ifCrit() {
        //计算是否暴击
        let crit = game.role.script.getCritChance();
        let i = 100 * Math.random();
        return i <= crit;
    }
    onCollisionEnter(monster, bullet) {
        // g_MSTDebuff.setPoision(monster, game.role.script.buff);
        cc.log("monster", monster);
        if (game.role.script.getPoison()) {
            g_Buff.addPoisonBuff(monster.ID);
        }
        if (game.role.script.getFire()) {
            g_Buff.addFireBuff(monster.ID);
        }
        if (this.ifCrit() == true) {
            //是否暴击
            let rate = game.role.script.getCritRate();
            let hurt = rate / 100 * bullet.attack;
            if (!monster.mom) {//判断是否为孩子怪物
                g_Monster.onHurt(monster, hurt, 5);
            } else {
                g_Monster.AI1003COnHurt(monster, hurt, 5);
            }
        } else {
            if (!monster.mom) {
                cc.log("hit mom");
                g_Monster.onHurt(monster, bullet.attack);
            } else {//孩子怪物要单独处理
                cc.log("hit c");
                g_Monster.AI1003COnHurt(monster, bullet.attack);
            }
            // g_Monster.onHurt(monster, bullet.attack);
        }
        let bulletID = bullet.ID;
        bullet.destroy();
        delete this.data[bulletID];
    }
    clear() {
        this.data = {};
        this.bulletLayer = null;
        this.bulletID = 100;
    }
    update(dt) {
        let tempBullet = {};
        for (let id in this.data) {
            let info = this.data[id];
            let angle = info.angle * Math.PI / 180;
            let speed = info.speed;
            let dx = Math.sin(-angle) * speed;
            let dy = Math.cos(-angle) * speed;
            info.bullet.x += dx;
            info.bullet.y += dy;
            info.bullet.angle = info.angle;
            info.life -= dt;
            if (info.life < 0) {
                info.bullet.destroy();
            }
            else {
                tempBullet[id] = info;
            }
        }
        this.data = tempBullet;
    }
}
window.g_Bullet = new bulletManager();